package internal

import (
	"context"
	"lol.com/feb214-game/nest.git/ginutils"
	"lol.com/feb214-game/nest.git/log"
	"net/http"
	"talent.com/cube/conf"
	"talent.com/cube/conf/g"
	"time"
)

var httpServer *http.Server

type Module struct{}

func (*Module) OnInit() {
	router := ginutils.GetLeafLoggerRouter()
	router.Use(ginutils.TokenChecker(g.ORM, false))
	v1 := router.Group("api/hundred")
	{
		v1.GET("ping", getPing) //查看服务
	}
	httpServer = &http.Server{
		Addr:    conf.ServerConfig().Server.ApiAddr,
		Handler: router,
	}
	go func() {
		if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatal("api server error:%s", err)
		}
	}()
}

func (*Module) OnDestroy() {
	cxt, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if httpServer != nil {
		_ = httpServer.Shutdown(cxt)
	}
	log.Info("admin server shutdown")
}

func (*Module) Run(closeSig chan bool) {
	//do nothing
}
